package com.language.points.multi.thread.num;

import java.util.Random;

/**
 * 多线程 - 减数据
 * @author yll
 * @version 1.0.0
 * @date 2024/04/12 13：55
 * @desc 减数据
 * */
public class NumSubThread implements Runnable {
    Numbers num = null;
    Integer count = 1000;
    public NumSubThread(Numbers num){
        this.num = num;
    }
    Random random = new Random();
    @Override
    public void run() {
        do{
//            synchronized (num) {
                num.subNum(1);
//            }
            try {
                Thread.currentThread().sleep(random.nextInt(10));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }while (count-- > 0);

        System.out.println(num);
    }

    public static void main(String[] args) {
        Numbers num = new Numbers(0);
        Thread thread1 = new Thread(new NumAddThread(num));
        Thread thread2 = new Thread(new NumSubThread(num));
        Thread thread3 = new Thread(new NumAddThread(num));
        Thread thread4 = new Thread(new NumSubThread(num));
        Thread thread5 = new Thread(new NumAddThread(num));
        Thread thread6 = new Thread(new NumSubThread(num));

        thread1.start();
        thread2.start();
//        thread3.start();
//        thread4.start();
//        thread5.start();
//        thread6.start();

    }
}
